﻿namespace EFS.RC.CommunicatorContext.Operators
{
	public partial class Search : EFS.RC.CommunicatorContext.Common.Pages.Controls.Control
	{
		#region Locals
		/// MaxResults
		public const System.Int32 MaxResults = 50;
		#endregion

		#region Delegates

		/// Open
		public delegate void OpenPartyDelegate(EFS.CC.HW.Views.BeneficiaryInfo beneficiaryInfo);
		public OpenPartyDelegate OpenParty;

		#endregion

		#region Constructors
		/// Implicit
		public Search()
		{
			InitializeComponent();

			// set label
			labelMoreResultsBottom.Content = System.String.Format("More than {0} results, please refine your search criteria!", MaxResults);

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listeners up
				UtilitiesService.HW_Contact_SearchCompleted += new System.EventHandler<ServiceReference.HW_Contact_SearchCompletedEventArgs>(HW_Contact_Search_Completed);
			}
		}
		#endregion

		#region Utilities
		/// Clear
		public void Clear()
		{
			dataGridHighwayResults.ItemsSource = null;
			labelMoreResultsBottom.Visibility = System.Windows.Visibility.Collapsed;
		}

		/// OpenSelectedParty
		private void OpenSelectedParty()
		{
			// get beneficiary
			EFS.CC.HW.Views.BeneficiaryInfo beneficiaryInfo = (EFS.CC.HW.Views.BeneficiaryInfo)dataGridHighwayResults.SelectedItem;
			if (null == beneficiaryInfo)
			{
				ShowErrorMessage("Please select a result entry");
				return;
			}

			// notify all
			if (null != OpenParty)
			{
				this.OpenParty(beneficiaryInfo);
			}

			// create url
			System.String url = "";
			if (null != beneficiaryInfo.PartyID && 0 != beneficiaryInfo.PartyID.Value)
			{
				// create url
				url = "http://exf-far-iway01/sapweb/launch-sso-prd.htm?PartyID=" + beneficiaryInfo.PartyID;
			}
			else
			{
				if (null != beneficiaryInfo.ApplicantID && 0 != beneficiaryInfo.ApplicantID.Value)
				{
					// create url
					url = "http://exf-far-iway01/sapweb/launch-sso-prd.htm?PartyID=" + beneficiaryInfo.ApplicantID;
				}
				else
				{
					ShowErrorMessage("Cannot open HW as both Party ID and Applicant ID are null.");
				}
			}

			// open
			if (EFS.Common.Verify.String(url))
			{
				OpenURL(url);
			}
		}
		
		/// Open URL
		private static void OpenURL(System.String url)
		{
			System.Windows.Browser.HtmlPage.Window.Navigate(
				new System.Uri(url),
				"__blank",
				"fullscreen=no,status=yes,toolbar=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=yes");
		}
		#endregion

		#region WS Callbacks
		/// HW results response handler
		private void HW_Contact_Search_Completed(object sender, ServiceReference.HW_Contact_SearchCompletedEventArgs e)
		{
			// enable button
			buttonSearch.IsEnabled = true;
			buttonSearch.Content = "Search";

			// show wait dialog
			//EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// check
			if (null != e.Error)
			{
				// show error
				ShowErrorMessage("Failed to retrieve data.", e.Error);

				// done
				return;
			}

			// clear
			Clear();

			// check
			if (null == e.Result)
			{
				// done
				return;
			}

			try
			{
				EFS.CC.HW.Views.BeneficiaryInfo[] array =
						EFS.Common.Serialization.Functions.Deserialize<EFS.CC.HW.Views.BeneficiaryInfo[]>(e.Result.ToString());

				// bind with datagrid
				dataGridHighwayResults.ItemsSource = array;

				//more results
				if (array.Length > MaxResults)
				{
					labelMoreResultsBottom.Visibility = System.Windows.Visibility.Visible;
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unexpected error occurred when retrieving data.", ex);
			}
		}
		#endregion

		#region Events
		/// Search
		private void buttonSearch_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// disable button
			buttonSearch.IsEnabled = false;
			buttonSearch.Content = "Searching ...";

			// show wait dialog
			//EFS.Common.UI.Controls.NotificationDialog.Dlg.Show("Please wait while fetching results");

			// clear
			Clear();

			//HW
			//System.String policyReferenceTemp;
			System.String policyReference = null;
			if (null != textBoxPolicyNumber.Text)
			{
				policyReference = textBoxPolicyNumber.Text;
			}

			// search
			UtilitiesService.HW_Contact_SearchAsync(
				textBoxFirstName.Text,
				textBoxLastName.Text,
				textBoxPostalCode.Text,
				textBoxPhoneNumber.Text,
				policyReference,
				MaxResults + 1);
		}

		/// Open party button
		private void openPartyButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			OpenSelectedParty();
		}

		/// dataGridHighwayResults_DoubleClick
		private void dataGridHighwayResults_DoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			OpenSelectedParty();
		}
		#endregion
	}

}